package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Cc
 * @version 1.0
 */
@Service
public class DtsStorageServiceImpl implements DtsStorageService {
    Logger logger = LoggerFactory.getLogger(DtsStorageServiceImpl.class);

    @Autowired
    private DtsStorageMapper dtsStorageMapper;


    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;


    @Override
    public DtsStorage create(String type, long size, String name, byte[] content) throws Exception {
        // 1. 当前文件上传到oss, 返回一个可访问url
        // 2. 创建一个对象: DtsStorage, 给这个对象添加各种属性.
        // 3. 调用mapper, 将上传的内容封装成实体类,存储到数据库当中
        // 4. 如果成功了,则直接返回刚才存储的对象.
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, name, content);
        logger.info("oss模块, 上传成功了, 获取到的url = {}", url);

        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setSize((int) size);
        dtsStorage.setName(name);
        String uuid = UUID.randomUUID().toString();
        dtsStorage.setKey(uuid);
        dtsStorage.setDeleted(false);
        dtsStorage.setUrl(url);
        dtsStorage.setType(type);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setUpdateTime(new Date());

        int row = dtsStorageMapper.insert(dtsStorage);
        return row == 0 ? null : dtsStorage;
    }

    @Override
    public IPage<DtsStorage> page(int page, int limit, String sort, String order) {
        // 参数校验
        // ......
        // page,第几页
        // limit, 每页显示的条数
        Page<DtsStorage> storagePage = new Page<>(page, limit);
        // 查询条件
        QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();

        // wrapper.orderBy(StringUtils.isEmpty(sort) ,StringUtils.isEmpty(sort) ? false : true, sort );
        wrapper.orderBy(!StringUtils.isEmpty(sort), !order.equals("desc"), sort);
        // if(StringUtils.isEmpty(order)){
        //     wrapper.orderByAsc(sort);
        // }else{
        //     wrapper.orderByDesc(sort);
        // }

        // 执行查询, 结果是一个分页对象
        Page<DtsStorage> resultPage = dtsStorageMapper.selectPage(storagePage, wrapper);
        return resultPage;
    }

    @Override
    public void deleteByKey(String key) {
        QueryWrapper<DtsStorage> wapper = new QueryWrapper<>();
        wapper.eq("`key`", key);

        //阿里云中删除文件
        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, key);
        //数据库中删除文件
        dtsStorageMapper.delete(wapper);
    }

    @Override
    public int update(DtsStorage dtsStorage) {
        dtsStorage.setUpdateTime(new Date());
        return dtsStorageMapper.updateById(dtsStorage);
    }
}
